<?php
namespace backend\modules\business\controllers;

use backend\controllers\BaseController;
use common\models\BusSupplieFinance;
use common\models\BusSupplier;
use yii\base\Object;
use yii\helpers\Url;
use yii\data\Pagination;
use common\extended\system\Util;

/**
 * 供应商财务日志管理
 * @author     chenxiaolin
 * @date        2016年2月22日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class SuppliefinanceController extends BaseController{
    public function init(){
        parent::init();
        $this->layout = '/alert';
    }
    /**
     * Index
     * @date: 2016年3月31日 
     * @author: chenxiaolin
     */
    public function actionIndex(){
        $this->redirect(Url::toRoute("supplier/list"));
    }
    /**
     * 往来记录
     * @date: 2016年2月22日 
     * @author: chenxiaolin
     */
    public function actionList(){
        $this->disableDebugtool();
        $supplier_id = intval($this->request->get('id',0));
        if($supplier_id <= 0){
           $this->error('参数错误！'); 
        }
        $query = BusSupplieFinance::find()->where('supplier_id = :id',[':id'=>$supplier_id]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => 20]);
        $finance = $query->offset($pages->offset)->limit($pages->limit)->orderBy('create_date DESC')->all();
        return $this->render('list',['data'=>$finance,'page'=>$pages]);
    }
    /**
     * 打款
     * @date: 2016年2月22日 
     * @author: chenxiaolin
     */
    public function actionPayment(){
        if(!$this->request->isPost){
            $this->disableDebugtool();
            return $this->render('payment');
        }
        //供应商ID
        $supplier_id = intval($this->request->get('id',0));
        if($supplier_id <= 0){
            echo "<script>parent.formResult('参数错误!','0')</script>";
            exit;
        }
        //获取数据
        $finance_money = $this->request->post('finance_money',0);
        $finance_give_money = $this->request->post('finance_give_money',0);
        $finance_remark = $this->request->post('finance_remark','');
        
        if(Util::utf8_strlen($finance_remark) > 20){
            echo "<script>parent.formResult('打款记录不超过20字!','0')</script>";
            exit;
        }
        if($finance_money == "" && $finance_give_money == ""){
            echo "<script>parent.formResult('货款、赠送必须填写一项！','0')</script>";
            exit;
        }
        if($finance_money != "" && $finance_give_money == ""){
            if(!is_numeric($finance_money) || strlen($finance_money) > 6){
                echo "<script>parent.formResult('货款为数字类型且最长6位数！','0')</script>";
                exit;
            }
            $finance_give_money = 0;
        }
        if($finance_give_money != "" && $finance_money == ""){
            if(!is_numeric($finance_give_money) || strlen($finance_give_money) > 6){
                echo "<script>parent.formResult('赠送为数字类型且最长6位数！','0')</script>";
                exit;
            }
            $finance_money = 0;
        }
        if($finance_give_money != "" || $finance_money != ""){
            if(!is_numeric($finance_give_money) || strlen($finance_give_money) > 6 || !is_numeric($finance_money) || strlen($finance_money) > 6){
                echo "<script>parent.formResult('货款、赠送为数字类型且最长6位数！','0')</script>";
                exit;
            }
        }
        //获取用户信息
        $userInfo = $this->userInfo;
        //查询供应商信息
        $supplier = BusSupplier::find()->where('supplier_id = :id and del_flag = 1',[':id'=>$supplier_id])->one();

        if(!$supplier){
            echo "<script>parent.formResult('供应商不存在！','0')</script>";
            exit;
        }
        //开启事务
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //保存供应商财务日志
            $finance = new BusSupplieFinance();
            $finance->supplier_id = $supplier_id;
            $finance->finance_type = '加货款';
            $finance->create_date = date('Y-m-d H:i:s');
            $finance->finance_money = $finance_money;
            $finance->finance_give_money = $finance_give_money;
            //变化后货款
            $finance->finance_balance = $supplier->supplier_balance + $finance_money;
            //变化后赠送
            $finance->finance_give_balance = $supplier->supplier_give_balance + $finance_give_money;
            $finance->finance_remark = $finance_remark;
            $finance->shop_id = $userInfo->shop_id;
            $finance->boss_id = $userInfo->boss_uid;
            
            $finance->save(false);
            //修改供应商：货款剩余、赠送剩余
            $supplier->supplier_balance = $supplier->supplier_balance + $finance_money;
            $supplier->supplier_give_balance = $supplier->supplier_give_balance + $finance_give_money;
            $supplier->save(false);
            //提交
            $transaction->commit();
            echo "<script>parent.formResult('打款成功!','1')</script>";
            /* echo "<script>";
            echo "top.location.href='".Url::toRoute('supplier/list')."'";
            echo "</script>"; */
        } catch (\Exception $e) {
            //回滚
            $transaction->rollBack();
            echo "<script>parent.formResult('打款失败!','0')</script>";
        }
    }
    /**
     * 扣款
     * @date: 2016年2月22日 
     * @author: chenxiaolin
     */
    public function actionDeductmoney(){
        if(!$this->request->isPost){
            $this->disableDebugtool();
            return $this->render('deductmoney');
        }
        //供应商ID
        $supplier_id = intval($this->request->get('id',0));
        if($supplier_id <= 0){
            echo "<script>parent.formResult('参数错误!','0')</script>";
            exit;
        }
        //获取数据
        $finance_money = $this->request->post('finance_money',0);
        $finance_give_money = $this->request->post('finance_give_money',0);
        $finance_remark = $this->request->post('finance_remark','');
        if(Util::utf8_strlen($finance_remark) > 20){
            echo "<script>parent.formResult('扣款记录不超过20字!','0')</script>";
            exit;
        }
        if($finance_money == "" && $finance_give_money == ""){
            echo "<script>parent.formResult('货款、赠送必须填写一项!','0')</script>";
            exit;
        }
        if($finance_money != "" && $finance_give_money == ""){
            if(!is_numeric($finance_money) || strlen($finance_money) > 6){
                echo "<script>parent.formResult('货款为数字类型且最长6位数!','0')</script>";
                exit;
            }
            $finance_give_money = 0;
        }
        if($finance_give_money != "" && $finance_money == ""){
            if(!is_numeric($finance_give_money) || strlen($finance_give_money) > 6){
                echo "<script>parent.formResult('赠送为数字类型且最长6位数!','0')</script>";
                exit;
            }
            $finance_money = 0;
        }
        if($finance_give_money != "" || $finance_money != ""){
            if(!is_numeric($finance_give_money) || strlen($finance_give_money) > 6 || !is_numeric($finance_money) || strlen($finance_money) > 6){
                echo "<script>parent.formResult('货款、赠送为数字类型且最长6位数!','0')</script>";
                exit;
            }
        }
        //获取用户信息
        $userInfo = $this -> userInfo;
        //查询供应商信息
        $supplier = BusSupplier::find()->where('supplier_id = :id and del_flag = 1',[':id'=>$supplier_id])->one();
        if(!$supplier){
            echo "<script>parent.formResult('供应商不存在!','0')</script>";
            exit;
        }
        //开启事务
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //保存供应商财务日志
            $finance = new BusSupplieFinance();
            $finance->supplier_id = $supplier_id;
            $finance->finance_type = '扣款';
            $finance->create_date = date('Y-m-d H:i:s');
            $finance->finance_money = $finance_money;
            $finance->finance_give_money = $finance_give_money;
            //变化后货款
            $finance->finance_balance = $supplier->supplier_balance - $finance_money;
            //变化后赠送
            $finance->finance_give_balance = $supplier->supplier_give_balance - $finance_give_money;
            $finance->finance_remark = $finance_remark;
            $finance->shop_id = $userInfo->shop_id;
            $finance->boss_id = $userInfo->boss_uid;
            $finance->save(false);
            //修改供应商：货款剩余、赠送剩余
            $supplier->supplier_balance = $supplier->supplier_balance - $finance_money;
            $supplier->supplier_give_balance = $supplier->supplier_give_balance - $finance_give_money;
            $supplier->save(false);
            //提交
            $transaction->commit();
            echo "<script>parent.formResult('扣款成功!','1')</script>";
            /* echo "<script>";
            echo "top.location.href='".Url::toRoute('supplier/list')."'";
            echo "</script>"; */
        } catch (\Exception $e) {
            //回滚
            $transaction->rollBack();
            echo "<script>parent.formResult('扣款失败!','0')</script>";
        }
    }
}